package com.onluy.frameworkcloud.system.factory;


import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public abstract class SingletonFactory {
    private static final Object LOCK = new Object();
    protected static final String IMPLEMENT_METHOD = "getInstance";
    private static Map<String, Object> instances = new HashMap();

    public static <T> T getInstance(Class<T> cls) {
        if (!instances.containsKey(cls.getName())) {
            Object arg0 = LOCK;
            synchronized (LOCK) {
                if (!instances.containsKey(cls.getName())) {
                    Object instance = genInstance(cls);
                    instances.put(cls.getName(), instance);
                }
            }
        }

        return (T)instances.get(cls.getName());
    }

    private static <T> T genInstance(Class<T> cls) {
        Object instance = null;

        try {
            Method method = cls.getDeclaredMethod("getInstance", new Class[0]);
            instance = method.invoke(cls, new Object[0]);
        } catch (IllegalArgumentException arg6) {
            ;
        } catch (IllegalAccessException arg7) {
            ;
        } catch (InvocationTargetException arg8) {
            ;
        } catch (SecurityException arg9) {
            ;
        } catch (NoSuchMethodException arg10) {
            try {
                instance = cls.newInstance();
            } catch (InstantiationException arg4) {
                ;
            } catch (IllegalAccessException arg5) {
                ;
            }
        }

        return (T)instance;
    }
}